// pages/cart/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data: {},
    empty:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var extConfigSync = app.getExtConfigSync();
    var that = this;
    var shop_info = app.getShopInfo();
    this.setData({ theme: extConfigSync.theme, shop_info: shop_info})
    
  },


  moreAct: function (e) {
    var that = this;
    var itemListArray = [];
    var goods_id = e.currentTarget.dataset.goods_id;
    var goods_spec_id = e.currentTarget.dataset.goods_spec_id;
    itemListArray.push({
      text: '删除此商品', act: function () {
        app.api("mall/cart/add", {
          goods_id: goods_id,
          goods_spec_id: goods_spec_id,
          count: 0,
        }, function (res) {
          if (res.code != 200) {
            app.alert(res.msg);
            return;
          }
          that.getcartByUserId();
        })
      }
    });
    var itemList = [];
    //console.log(itemListArray);
    for (var i in itemListArray) {
      itemList.push(itemListArray[i].text);
    }
    wx.showActionSheet({
      itemList: itemList,
      success: function (res) {
        console.log(itemListArray[res.tapIndex]);
        itemListArray[res.tapIndex]['act']();
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },

  // 购物车
  getcartByUserId: function () {
    var that = this;
    app.api("mall/cart/getcartByUserId", { shop_id: this.data.shop_info.id }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
      } else if (res.data) {
        that.setData({ shoppingCartList: res.data });
        // console.log("shoppingCartList",that.data.shoppingCartList);
      }
      if (res.data.count==0){
        that.setData({"empty":1});
      }
    })
  },

  //商品数量减
  minus: function (e) {
    var id = e.currentTarget.dataset.id;
    var shoppingCartList = this.data.shoppingCartList;
    if (shoppingCartList.list[id].count < 1) {
      shoppingCartList.list[id].count = 0;
    } else {
      shoppingCartList.list[id].count--;
    }
    this.setData({ shoppingCartList: shoppingCartList });
    this.addToCart({
      count: shoppingCartList.list[id].count,
      goods_spec_id: e.currentTarget.dataset.goods_spec_id,//  $(input).attr("goods_spec_id"),
      goods_id: e.currentTarget.dataset.goods_id,
    })

  },
  //商品数量加
  plus: function (e) {
    var id = e.currentTarget.dataset.id;
    var shoppingCartList = this.data.shoppingCartList;
    shoppingCartList.list[id].count++;
    this.setData({ shoppingCartList: shoppingCartList });
    this.addToCart({
      count: shoppingCartList.list[id].count,
      goods_spec_id: e.currentTarget.dataset.goods_spec_id,//  $(input).attr("goods_spec_id"),
      goods_id: e.currentTarget.dataset.goods_id,
    })
  },
  //商品数量
  changeCount: function (e) {
    var id = e.currentTarget.dataset.id;
    var count = e.detail.value;
    var shoppingCartList = this.data.shoppingCartList;
    shoppingCartList.list[id].count = count;
    this.setData({ shoppingCartList: shoppingCartList });
    this.addToCart({
      count: count,
      goods_spec_id: e.currentTarget.dataset.goods_spec_id,//  $(input).attr("goods_spec_id"),
      goods_id: e.currentTarget.dataset.goods_id,
    })

  },

  //加入购物车
  addToCart: function (post, callback) {
    wx.showLoading();
    var that = this;
    app.api("/mall/cart/add", post, function (res) {
      wx.hideLoading();
      if (res.code != 200) {
        app.alert(res.msg);
      } else {
        callback ? callback(res) : '';
        that.setData({ shoppingCartList: res.data });

      }
    })
  },
  //清空购物车
  clearCart: function () {
    wx.showLoading();
    var that = this;
    app.api("mall/cart/clearCart", { shop_id: this.data.shop_info.id }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
      } else {
        //phpkitApp.alert("恭喜,成功清空购物车!");
        wx.showToast({
          title: "恭喜,成功清空购物车!",
          icon: 'success'
        });
        that.setData({ shoppingCartList: {
          nextText:'去结算'
        } });
      }
      wx.hideLoading();
    })
  },
  //去结算
  settle_accounts: function () {
    wx.navigateTo({
      url: "/pages/checkout/index?shop_id=" + this.data.shop_info.id
    });
  },
  //选规格
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    app.getUserInfo(function (userInfo) {
      that.getcartByUserId();
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getcartByUserId();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})